package com.hod.client.reserves;

import com.google.gwt.gears.client.database.DatabaseException;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.DateFieldDef;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.FloatFieldDef;
import com.gwtext.client.data.IntegerFieldDef;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.grid.BaseColumnConfig;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtextux.client.data.PagingMemoryProxy;
import com.hod.client.backend.orm.client.GearsDataStore;
import com.hod.client.backend.orm.client.GearsQuery;
import com.hod.client.frontend.AbstractGrid;

public class ReservationStateGrid extends AbstractGrid {
	private static RecordDef recordDef = new RecordDef(
            new FieldDef[]{
                    new IntegerFieldDef("idReservationState"),
                    new StringFieldDef("name")                    
               }
    );
	@Override
	protected void getData() {
		PagingMemoryProxy proxy = null;
		try {
			GearsQuery q = this.ds.getNamedQuery(ReservationState.FIND_ALL);
			ReservationState[] data = (ReservationState[]) q.execute();
			ReservationState rs = null;
			Object[][]data2 = new Object[data.length][];
			for (int i = 0; i < data.length; i++) {
                rs = data[i];	               
                data2[i] = new Object[]{rs.getIdReservationState(),rs.getName()};
            }
			proxy = new PagingMemoryProxy(data2);
			
			
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			//Window.alert(e.getMessage());
		} 

        ArrayReader reader = new ArrayReader(recordDef);
        store = new Store(proxy, reader);
        store.load(0,numberRecords);
        setStore(store);

	}

	@Override
	protected void getData(String id) {
		// TODO Auto-generated method stub
        getData("");
	}
	
	public ReservationStateGrid(boolean small,GearsDataStore ds) {
    	this.ds = ds;
    	columns = new BaseColumnConfig[]{
                new ColumnConfig("idReservationState", "idReservationState", 60, true, null, "idReservationState"),    	
                new ColumnConfig("name", "name", 60, true, null, "name")
        };
    	
    	getData();

        ColumnModel columnModel = getColumnConfigs();
        setColumnModel(columnModel);

        setFrame(true);
        setStripeRows(true);
        setIconCls("grid-icon");      

        createPopUp();
        createPager(); 
    }

}
